package com.frank.activedatabus.broadcast;

import com.frank.activedatabus.Core.LiveEventBusCore;
import com.frank.activedatabus.Core.Observer;
import ohos.aafwk.ability.ILifecycle;
import ohos.aafwk.ability.Lifecycle;
import ohos.aafwk.ability.LifecycleStateObserver;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.rpc.RemoteException;

/**
 * @version 1.0.1
 * @description:公共事件管理中心
 * @program: Gallery
 * @Author xiaozhijun
 * @Date 2020/12/23 10:30
 */
public class BroadCastManager {

    private static final String TAG = "CommonEventManagerCenter";

    private static class CommonEventManagerHolder {
        public static BroadCastManager instance = new BroadCastManager();
    }

    private BroadCastCacheManage eventSubjectCacheManage;

    private BroadCastManager() {
        eventSubjectCacheManage = new BroadCastCacheManage();
    }

    public static BroadCastManager getInstance() {
        return CommonEventManagerHolder.instance;
    }


    /**
     * 注册公共事件
     *
     * @param listener 回调接口
     * @param action   订阅主题
     * @param priority 优先级 默认为0
     */
    public void registerCommonEvent(Observer listener, String action, int priority, ILifecycle own) {
        if (action != null && !eventSubjectCacheManage.contains(listener, action)) {
            priority = priority >= 0 ? priority : 0;
            CommonEventSubscriber subscriber = SubscriberObtain.getSubscriber(action, priority);
            try {
                //订阅公共事件
                CommonEventManager.subscribeCommonEvent(subscriber);
                eventSubjectCacheManage.add(listener, action, subscriber);

                own.getLifecycle().addObserver((LifecycleStateObserver) (event, intent) -> {
                    if (event.name().equals(Lifecycle.Event.ON_STOP.name())) {
                        unRegisterCommonEvent(listener, action);
                    }
                });
                LiveEventBusCore.get().getChannel(action).observe(own, listener);

            } catch (RemoteException e) {
                e.printStackTrace();
//                LogUtils.e("regiysterCommonEvent exception = " + e.getMessage());
            }
        }
    }


    /**
     * 注册公共事件(不带优先级)
     *
     * @param listener 回调接口
     * @param action   订阅主题
     */
    public void registerCommonEvent(Observer listener, String action, ILifecycle own) {
        registerCommonEvent(listener, action, 0, own);
    }

    /**
     * 注册多个公共事件
     *
     * @param listener 回调接口
     * @param actions  订阅主题
     */
    public void registerCommonEvent(Observer listener, String[] actions, ILifecycle own) {
        if (actions != null) {
            for (String action : actions) {
                registerCommonEvent(listener, action, own);
            }
        }
    }


    /**
     * 取消注册公共事件
     *
     * @param listener
     * @param action
     */
    public void unRegisterCommonEvent(Observer listener, String action) {
        if (eventSubjectCacheManage.contains(listener, action)) {
            try {
                //取消订阅
                CommonEventSubscriber subscriber = eventSubjectCacheManage.remove(listener, action);
                if (subscriber != null) {
                    CommonEventManager.unsubscribeCommonEvent(subscriber);
                }
                LiveEventBusCore.get().getChannel(action).removeObserver(listener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
}
